package com.example.myfirstapplication

import android.app.*
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import com.example.myfirstapplication.layout.TabBarLayout
import com.example.myfirstapplication.utils.BaseActivity
import com.example.myfirstapplication.utils.Tag
import com.example.myfirstapplication.utils.goActivity
import com.example.myfirstapplication.utils.myToast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity(), View.OnClickListener {
    /*    private lateinit var btnMain: Button
        private lateinit var btn2Main: Button
        private lateinit var txtMain: TextView*/
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val actionBar = supportActionBar
        actionBar?.hide()
/*        txtView_topbartitle.text="我的APP"
        btnLeft_topbartitle.visibility=View.INVISIBLE
        btnRight_topbartitle.setOnClickListener{
            myToast(this,"你点击了设置按钮")
        }*/
        topBar.hideLeftBar()
        topBar.setRightBtnText("分享")
        topBar.setTitle("标题")
        topBar.setRightBtnOnClickCallback {
            myToast(this, "我点击了分享按钮！")
        }

        Log.i(Tag, getMobileInfo())
        //    txtMain = findViewById(R.id.txtView_mainAct)
        txtView_mainAct.text = getMobileInfo()

        //   btnMain = findViewById(R.id.btn_mainAct)
        btn_mainAct.setOnClickListener(this)
        //   btn2Main = findViewById(R.id.btn2_mainAct)
        btn2_mainAct.setOnClickListener(this)
        btnViewLayout_mainAct.setOnClickListener(this)
        btnCommonWidget_mainAct.setOnClickListener(this)
        Log.i(Tag, "MainActivity: " + this.toString())
        btnRecyclerView_mainAct.setOnClickListener(this)
        btnListView_mainAct.setOnClickListener(this)
        btnCommonWidget2_mainAct.setOnClickListener(this)
        btnTabBar_mainAct.setOnClickListener(this)
        btnViewPagerLayout_mainAct.setOnClickListener(this)
        btnFileOP_mainAct.setOnClickListener(this)
        btnBroadcast.setOnClickListener(this)
        showNotification()
        btnPhoto.setOnClickListener(this)
        btnAudio.setOnClickListener(this)
        btnVideo.setOnClickListener(this)
        btnThread.setOnClickListener(this)
        btnRuntimePermission.setOnClickListener(this)
        btnWebView.setOnClickListener(this)
        btnNetwork.setOnClickListener(this)
    }

    private fun showNotification() {
        val notificatiobManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel("normal", "一般通知", NotificationManager.IMPORTANCE_DEFAULT)
            notificatiobManager.createNotificationChannel(channel)
        }
        btnNotification.setOnClickListener {
            val intent = Intent(this, LoginActivity::class.java)
            intent.putExtra("myId", "my id is 7")
            val pendingIntent =
                PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
            val notification = NotificationCompat.Builder(this, "normal")
                .setContentTitle("请登录！")
                .setContentText("点击通知，进行登录！")
/*                .setStyle(
                    NotificationCompat.BigPictureStyle().bigPicture(
                        BitmapFactory.decodeResource(
                            resources,
                            R.mipmap.ic_launcher_round
                        )
                    )
                )*/
                .setStyle(
                    NotificationCompat.BigTextStyle()
                        .bigText("Learn how to build notifications, send and sync data, and use voice actions. Get the official Android IDE and developer tools to build apps for Android.")
                )
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setLargeIcon(
                    BitmapFactory.decodeResource(
                        resources,
                        R.mipmap.ic_launcher_round
                    )
                )
                .build()
            notificatiobManager.notify(
                1, notification
            )
        }
    }

    private fun getMobileInfo(): String? {
        var info = "手机品牌：" + android.os.Build.MODEL
        info += "\n操作系统：Android" + android.os.Build.VERSION.RELEASE
        return info
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val revData = data?.extras
        when (requestCode) {
            1 -> {
                when (resultCode) {
                    Activity.RESULT_OK -> {
                        val inputData = revData?.getString("inputData")
                        txtView_mainAct.text = inputData
                        Log.i(Tag, "主界面得到的回调数据：" + inputData)
                    }
                }
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_mainAct -> {
                var intentObj = Intent(this@MainActivity, SecondActivity::class.java)
                var data = Bundle()
                data.putString("myData", "这是我要传递给子界面的数据！")
                data.putString("myDataKey", "这是我要传递给子界面的第二个数据！")
                intentObj.putExtras(data)
                //           startActivity(intentObj)
                startActivityForResult(intentObj, 1)
            }
            R.id.btn2_mainAct -> {
                AlertDialog.Builder(this).apply {
                    setTitle("提醒")
                    setMessage("你确定打开此网站吗？")
                    setCancelable(true)
                    setPositiveButton("确定") { dialog, which ->
                        val intentObj = Intent(Intent.ACTION_VIEW)
                        intentObj.data = Uri.parse("http://api.nieyu.com/weather/")
                        startActivity(intentObj)
                    }
                    setNegativeButton("取消") { dialog, which ->
                        dialog.dismiss()
                    }
                    setNeutralButton("我不确定") { dialog, which ->
                        dialog.dismiss()
                    }
                    setOnDismissListener {
                        Log.i(Tag, "对话框被隐藏，dismiss处理")
                    }
                    setOnCancelListener {
                        Log.i(Tag, "对话框被隐藏，cancel处理")
                    }
                    show()
                }
            }
            R.id.btnViewLayout_mainAct -> {
/*                val intentObj = Intent(this, LayoutActivity::class.java)
                startActivity(intentObj)*/
                goActivity(
                    this,
                    LayoutActivity::class.java
                )
            }
            R.id.btnCommonWidget_mainAct -> {
/*                val intentObj = Intent(this, CommonWidget::class.java)
                startActivity(intentObj)*/
                goActivity(
                    this,
                    CommonWidget::class.java
                )
            }
            R.id.btnListView_mainAct -> {
                goActivity(
                    this,
                    ListViewActivity::class.java
                )
            }
            R.id.btnRecyclerView_mainAct -> {
                goActivity(
                    this,
                    RecyclerViewActivity::class.java
                )
            }
            R.id.btnCommonWidget2_mainAct -> {
                goActivity(
                    this,
                    CommonWidget2::class.java
                )
            }
            R.id.btnTabBar_mainAct -> {
                goActivity(
                    this,
                    TabBarLayout::class.java
                )
            }
            R.id.btnViewPagerLayout_mainAct -> {
                goActivity(
                    this,
                    ViewPagerTabLayout::class.java
                )
            }
            R.id.btnFileOP_mainAct -> {
                goActivity(
                    this,
                    FileOPActivity::class.java
                )
            }
            R.id.btnBroadcast -> {
                goActivity(
                    this,
                    BroadcastActivity::class.java
                )
            }
            R.id.btnPhoto -> {
                goActivity(
                    this,
                    PhotoActivity::class.java
                )
            }
            R.id.btnAudio -> {
                goActivity(
                    this,
                    AudioActivity::class.java
                )
            }
            R.id.btnVideo -> {
                goActivity(
                    this,
                    VideoActivity::class.java
                )
            }
            R.id.btnThread -> {
                goActivity(
                    this,
                    ThreadActivity::class.java
                )
            }
            R.id.btnRuntimePermission -> {
                goActivity(
                    this,
                    RuntimePermissionActivity::class.java
                )
            }
            R.id.btnWebView -> {
                goActivity(
                    this,
                    WebViewActivity::class.java
                )
            }
            R.id.btnNetwork -> {
                goActivity(
                    this,
                    NetworkActivity::class.java
                )
            }
        }
    }
/*    override fun onStart() {
        super.onStart()
        Log.i(Tag, "onStart: activity")
    }*/
}
